package com.heritage.controller;

import com.heritage.dto.response.ApiResponse;
import com.heritage.dto.response.HeritageResponse;
import com.heritage.service.HeritageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/heritage")
public class HeritageController {

    @Autowired
    private HeritageService heritageService;

    @GetMapping("/list")
    public ResponseEntity<?> getAllHeritages() {
        try {
            List<HeritageResponse> heritages = heritageService.getAllHeritages();
            return ResponseEntity.ok(ApiResponse.success(heritages));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getHeritageById(@PathVariable Long id) {
        try {
            HeritageResponse heritage = heritageService.getHeritageById(id);
            return ResponseEntity.ok(ApiResponse.success(heritage));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }

    @PostMapping("/{id}/like")
    public ResponseEntity<?> toggleLike(@PathVariable Long id) {
        try {
            boolean isLiked = heritageService.toggleLike(id);
            String message = isLiked ? "点赞成功" : "取消点赞成功";
            return ResponseEntity.ok(ApiResponse.success(message, isLiked));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
}
